let ipt_account = document.getElementById("ipt_account");
let ipt_password = document.getElementById("ipt_password");
let ipt_cfm_password = document.getElementById("ipt_cfm_password");

function save() {
	let account = ipt_account.value;
	let password = ipt_password.value;
	let cfm_password = ipt_cfm_password.value;
	//验证表单
	if(account.trim().length === 0) {
		alert("请输入账号");
		return;
	}
	
	if(password.trim().length < 6) {
		alert("密码至少要求6位");
		return;
	}
	//两次密码必须一致
	if(password !== cfm_password) {
		alert("两次密码必须一致");
		return;
	}
	
	/*验证账号是否重复
		1.从localStorage中去获取用户注册用的数组
		2.遍历数组进行账号比对
	*/
	let users = JSON.parse(localStorage.getItem("users"));
	console.log(users);
	if(!users) {
		users = new Array();
	}
	
	for(let index in users) {
		let item = users[index];
		if(item.account === account) {
			alert("该账号已被注册");
			return;
		}
	}
	console.log(typeof(users));
	users.push({account, password});//ES6支持的简写方式，key和value都同名
	localStorage.setItem("users", JSON.stringify(users));//保存到localStorage中
	alert("注册成功");
	location.href = "001.html";
}
function cancel(){
	location.href = "001.html";
}